package com.ruoyi.agent.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.agent.domain.dto.AmagentTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.agent.mapper.AmAgentMapper;
import com.ruoyi.agent.domain.AmAgent;
import com.ruoyi.agent.service.IAmAgentService;

/**
 * 经纪人信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-26
 */
@Service
public class AmAgentServiceImpl implements IAmAgentService 
{
    @Autowired
    private AmAgentMapper amAgentMapper;

    /**
     * 查询经纪人信息
     * 
     * @param id 经纪人信息ID
     * @return 经纪人信息
     */
    @Override
    public AmAgent selectAmAgentById(Long id)
    {
        return amAgentMapper.selectAmAgentById(id);
    }

    /**
     * 查询经纪人信息列表
     * 
     * @param amAgent 经纪人信息
     * @return 经纪人信息
     */
    @Override
    public List<AmAgent> selectAmAgentList(AmAgent amAgent)
    {
        return amAgentMapper.selectAmAgentList(amAgent);
    }

    /**
     * 经纪人层级关系
     *
     * @param id 经纪人id
     * @return 经纪人信息集合
     */
    @Override
    public List<AmagentTree> load(Long id) {
        // 查询全部经纪人
        List<AmagentTree> list = amAgentMapper.selectAmAgentTreeList();
        return buildTree(list,id);
    }


    private List<AmagentTree> buildTree(List<AmagentTree> list , Long parentId){
        List<AmagentTree> amagentTrees = new ArrayList<AmagentTree>();
        for (AmagentTree amagentTree : list) {

                  Long id = amagentTree.getId();
                  Long pid = amagentTree.getParentId();
                  if (parentId.equals(pid)) {
                          List<AmagentTree> menuLists = buildTree(list, id);

                      amagentTree.setChildren(menuLists);
                      amagentTrees.add(amagentTree);
                      }
              }
        return amagentTrees;
    }
    /**
     * 新增经纪人信息
     * 
     * @param amAgent 经纪人信息
     * @return 结果
     */
    @Override
    public int insertAmAgent(AmAgent amAgent)
    {
        amAgent.setCreateTime(DateUtils.getNowDate());
        return amAgentMapper.insertAmAgent(amAgent);
    }

    /**
     * 修改经纪人信息
     * 
     * @param amAgent 经纪人信息
     * @return 结果
     */
    @Override
    public int updateAmAgent(AmAgent amAgent)
    {
        return amAgentMapper.updateAmAgent(amAgent);
    }

    /**
     * 批量删除经纪人信息
     * 
     * @param ids 需要删除的经纪人信息ID
     * @return 结果
     */
    @Override
    public int deleteAmAgentByIds(Long[] ids)
    {
        return amAgentMapper.deleteAmAgentByIds(ids);
    }

    /**
     * 删除经纪人信息信息
     * 
     * @param id 经纪人信息ID
     * @return 结果
     */
    @Override
    public int deleteAmAgentById(Long id)
    {
        return amAgentMapper.deleteAmAgentById(id);
    }
}
